package com.teamwork;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import java.lang.*;



public class OnlineExamSystem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 显示登录选项
        System.out.println("欢迎使用在线考试系统");
        System.out.println("请选择您的身份：");
        System.out.println("1. 学生登录");
        System.out.println("2. 教师登录");
        System.out.println("0. 退出");
        System.out.print("请输入选项：");

        int role = scanner.nextInt();
        scanner.nextLine();  // 消耗换行符
        String username, password;
        if(role == 0){
            System.exit(0);
        }

        // 获取用户名和密码
        System.out.print("请输入用户名：");
        username = scanner.nextLine();

        System.out.print("请输入密码：");
        password = scanner.nextLine();

  
        if (role == 1) {
            //学生登录判定
            try (Connection conn = DBUtil.getConnection()) {
            String sql = "SELECT SId, SName, SRname, CLId FROM student WHERE SName = ? AND Spwd = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                int studentId = rs.getInt("SId");
                String realName = rs.getString("SRname");
                int classId = rs.getInt("CLId");
                System.out.println("登录成功！");
                studentSystem(username);
            } else {
                 System.out.println("登录失败，用户名或密码错误！");
                 OnlineExamSystem.main(null);
            }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } else {
        //教师登录判定
            try (Connection conn = DBUtil.getConnection()) {
            String sql = "SELECT TId, TName, TRname, CLId FROM teacher WHERE TName = ? AND Tpwd = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                int teacherId = rs.getInt("TId");
                String realName = rs.getString("TRname");
                int classId = rs.getInt("CLId");
                System.out.println("登录成功！");
                teacherSystem(username);
            } else {
                System.out.println("登录失败，用户名或密码错误！");
                OnlineExamSystem.main(null);
            }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        scanner.close();
    }

    // 学生系统
    private static void studentSystem(String username) {
        System.out.println("欢迎，学生 " + username + "！");
        System.out.println("学生系统。");
        System.out.println("1.线上考试");
        System.out.println("2.查看成绩");
        System.out.println("0.退出");

        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        if(choice == 0){
            System.exit(0);
         }

        if(choice == 1){
            studentonlinetsting(username);
        }
        else if(choice == 2){
            studentcheckscore(username);
        }

        scanner.close();
    }


        //线上考试系统
        public static void studentonlinetsting(String username) {
            String sql = "SELECT SId, SName, SRname, CLId FROM student WHERE SName = ?";
            Scanner scanner9 = new Scanner(System.in);
            System.out.println("请试卷ID：");
            int examId = scanner9.nextInt();
            String sql1 = "SELECT CHN FROM exam WHERE EId = examid";
            try (Connection conn = DBUtil.getConnection()){
                //String sql = "SELECT SId, SName, SRname, CLId FROM student WHERE SName = ?";
                PreparedStatement pstmt = conn.prepareStatement(sql);
                ResultSet rs = pstmt.executeQuery();
                int studentId = rs.getInt("SId");
                //String sql1 = "SELECT CHN FROM exam WHERE EId = examid";
                PreparedStatement pstmt1 = conn.prepareStatement(sql1);
                ResultSet rs1 = pstmt1.executeQuery();
                int choicenumber = rs1.getInt("CHN");
                for(int i = 0; i < choicenumber; i++){
                    System.out.println("请输入选择题编号：");
                    int questionId = scanner9.nextInt();
                    System.out.println("请输入答案：");
                    String studentAnswer = scanner9.next();
                    SubmitExamAnswer studenttest = new SubmitExamAnswer();
                    studenttest.submitAnswer(studentId, examId, questionId, studentAnswer);
                }
              /*String sql = "SELECT * FROM examchoice WHERE eid = ?";
                PreparedStatement pstmt = conn.prepareStatement(sql);
                pstmt.setInt(1, examId); // 设置examid参数

                ResultSet rs = pstmt.executeQuery();

                while (rs.next()) {
                    // 处理每一行数据
                    int id = rs.getInt("id");
                    String name = rs.getString("name");
                    String sql2 = "SELECT CHId FROM examchoice WHERE EId = examId";
                    PreparedStatement pstmt2 = conn.prepareStatement(sql2);
                    ResultSet rs2 = pstmt2.executeQuery();
                    int questionId = rs2.getInt("CHId");
                    
                    System.out.println("请输入答案：");
                    String studentAnswer = scanner9.next();
                    SubmitExamAnswer studenttest = new SubmitExamAnswer();
                    studenttest.submitexamanswer(studentId, examId, questionId, studentAnswer);
                }*/
        } catch (SQLException e) {
            e.printStackTrace();
        }
        AutoGradeExam autoGradeExam = new AutoGradeExam();
        autoGradeExam.Garding();

        studentSystem(username);
        scanner9.close();
    }
    
    
        //学生查分
         public static void studentcheckscore(String username) {
            Scanner scanner8 = new Scanner(System.in);
            System.out.println("请输入学生ID以查分：");
            int studentId = scanner8.nextInt();
    
            CheckScore checkScore = new CheckScore();
            checkScore.checkscore(studentId);
            
            studentSystem(username);
            scanner8.close();
        }



    // 教师系统
    private static void teacherSystem(String username) {
        System.out.println("欢迎，教师 " + username + "！");
        System.out.println("教师系统。");
        System.out.println("1.学生数据修改");
        System.out.println("2.教师数据修改");
        System.out.println("3.试卷数据修改");
        System.out.println("4.查看成绩");
        System.out.println("5.组卷");
        System.out.println("0.返回登录系统");
        
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        if(choice == 0){
            OnlineExamSystem.main(null);
        }
        if(choice == 1){
            studentDataModification(username);
        }
        else if(choice == 2){
            teacherDataModification(username);
        }
        else if(choice == 3){   
            examPaperDataModification(username);          
        }
        else if(choice == 4){
            teacherCheckScore(username);
        }
        else if(choice == 5){
            teacherMakePaper(username);
        }

        scanner.close();
    }


    // 学生数据修改系统
    private static void studentDataModification(String username) {
        System.out.println("欢迎，教师 " + username + "！");
        System.out.println("学生数据修改系统。");
        System.out.println("1.学生数据增加");
        System.out.println("2.学生数据删除");
        System.out.println("0.返回教师系统");

        Scanner scanner1 = new Scanner(System.in);
        int choice1 = scanner1.nextInt();
        if (choice1 == 0) {
            teacherSystem(username);
        } else if (choice1 == 1) {
            Scanner scanner2 = new Scanner(System.in);
            System.out.print("请输入学生用户名：");
            String sName = scanner2.nextLine();
            System.out.print("请输入学生密码：");
            String spwd = scanner2.nextLine();
            System.out.print("请输入学生真实姓名：");
            String sRname = scanner2.nextLine();
            System.out.print("请输入班级ID：");
            int clId = scanner2.nextInt();
            System.out.print("请输入学生ID：");
            int sId = scanner2.nextInt(); // 添加这一行，获取学生ID
            //scanner2.close();

            // 调用添加学生数据的方法
            xsxxzj studentDataAddition = new xsxxzj();
            studentDataAddition.addStudent(sId, sName, spwd, sRname, clId); 

            //展示删除后的数据库
            xsxxzs studentDataDisplay = new xsxxzs();
            studentDataDisplay.displayStudentData();

            teacherSystem(username);
            scanner2.close();
        } else if (choice1 == 2) {
            Scanner scanner3 = new Scanner(System.in);
            System.out.print("请输入要删除的学生信息的学号：");
            int sId = scanner3.nextInt();
            //scanner3.close();
            
            // 调用删除学生数据的方法
            xsxxsc studentDataDeletion = new xsxxsc();
            studentDataDeletion.deleteStudent(sId);
            
            //展示删除后的数据库
            xsxxzs studentDataDisplay = new xsxxzs();
            studentDataDisplay.displayStudentData();

            teacherSystem(username);
            scanner3.close();
        } else {
            System.out.println("无效的选择，请重新输入。");
            studentDataModification(username); // 递归调用，返回上一层系统
        }
        scanner1.close();
    }

    // 教师数据修改系统
    private static void teacherDataModification(String username) {
        System.out.println("欢迎，教师 " + username + "！");
        System.out.println("教师数据修改系统。");
        System.out.println("1.教师数据增加");
        System.out.println("2.教师数据删除");
        System.out.println("0.返回教师系统");
        
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        if (choice == 0) {
            teacherSystem(username);
        }
        else if (choice == 1) {
            Scanner scanner4 = new Scanner(System.in);
            System.out.print("请输入教师姓名：");
            String tname = scanner4.nextLine();
            System.out.print("请输入教师密码：");
            String tpwd = scanner4.nextLine();
            System.out.print("请输入教师真实姓名：");
            String tRname = scanner4.nextLine();
            System.out.print("请输入班级ID：");
            int clId = scanner4.nextInt();
            System.out.print("请输入教师ID：");
            int tId = scanner4.nextInt(); // 添加这一行，获取教师ID

            // 教师数据增加
            jsxxzj teacherDataAddition = new jsxxzj();
            teacherDataAddition.addTeacher(tId, tname, tpwd, tRname, clId); // 修改这一行，传入教师ID

             //展示删除后的数据库
            jsxxzs teacherDataDisplay = new jsxxzs();
            teacherDataDisplay.displayTeacherData();

            teacherSystem(username);
            scanner4.close();
        }
        else if (choice == 2) {
            Scanner scanner6 = new Scanner(System.in);
            System.out.print("请输入要删除的教师信息的ID：");
            int tId = scanner6.nextInt();
            
            // 教师数据删除
            jsxxsc teacherDataDeletion = new jsxxsc();
            teacherDataDeletion.deleteTeacher(tId);

            //展示删除后的数据库
            jsxxzs teacherDataDisplay = new jsxxzs();
            teacherDataDisplay.displayTeacherData();
            
            teacherSystem(username);
            scanner6.close();  
        }else {
            System.out.println("无效的选择，请重新输入。");
            teacherDataModification(username); // 递归调用，返回上一层系统
        }
        scanner.close();
    }


    // 试卷数据修改系统
    private static void examPaperDataModification(String username) {
        System.out.println("欢迎，教师 " + username + "！");
        System.out.println("试卷数据修改系统。");
        System.out.println("1.试卷数据增加");
        System.out.println("2.试卷数据删除");
        System.out.println("0.返回教师系统");
        Scanner scanner2 = new Scanner(System.in);
        
        int choice = scanner2.nextInt();
        if (choice == 0) {
            teacherSystem(username);
        }
        else if (choice == 1) {
            Scanner scanner7 = new Scanner(System.in);
            System.out.print("请输入选择题ID ：");
            String cHID = scanner7.nextLine();
            System.out.print("请输入学科ID：");
            String cno = scanner7.nextLine();
            System.out.print("请输入题目内容：");
            String item = scanner7.nextLine();
            System.out.print("请输入选项A：");
            String cHA = scanner7.nextLine();
            System.out.print("请输入选项B：");
            String cHB = scanner7.nextLine();
            System.out.print("请输入选项C：");
            String cHC = scanner7.nextLine();
            System.out.print("请输入选项D：");
            String cHD = scanner7.nextLine();
            System.out.print("请输入正确答案：");
            String cHKey = scanner7.nextLine();

            // 题库数据增加
            tkzj examPaperDataAddition = new tkzj();
            examPaperDataAddition.addChoice(cHID ,cno, item, cHA, cHB , cHC , cHD , cHKey);

            //展示增加后的数据库
            tkzs examPaperDataDisplay = new tkzs();
            examPaperDataDisplay.displayExamchoiceData();
            
            teacherSystem(username);
            scanner7.close();
        }
        else if (choice == 2) {
            Scanner scanner8 = new Scanner(System.in);
            System.out.print("请输入要删除的试题信息的ID：");
            int tId = scanner8.nextInt();
            
            // 题库数据删除
            tksc examPaperDataDeletion = new tksc();
            examPaperDataDeletion.deleteChoice(tId);

            //展示删除后的数据库
            tkzs examPaperDataDisplay = new tkzs();
            examPaperDataDisplay.displayExamchoiceData();
            
            teacherSystem(username);
            scanner8.close();
        }
        else {
            System.out.println("无效的选择，请重新输入。");
            examPaperDataModification(username); // 递归调用，返回上一层系统
        }
        scanner2.close();
    }

    // 教师分数查询系统
    private static void teacherCheckScore(String username) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入您的ID：");
        int teacherId = scanner.nextInt();
        System.out.print("请输入班级ID：");
        int classId = scanner.nextInt();
        
        TeacherScoreQuery teacherScoreQuery = new TeacherScoreQuery();
        teacherScoreQuery.TeacherQuery(teacherId,classId);
        teacherSystem(username);
        scanner.close();
    }

    // 组卷系统
    private static void teacherMakePaper(String username) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入需要选择题的数量: ");
        int num = scanner.nextInt();
        RandomQuestions randomQuestions = new RandomQuestions();
        randomQuestions.questions(num);
        
        teacherSystem(username);
        scanner.close();
    }
}
